package cn.kgc.javaweb.servlet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONWriter;
import netscape.javascript.JSObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;

/**
 * @author: mosin
 * @version: v1.0  2022/5/26
 */
@WebServlet("/user")
public class UserServlet extends BaseServlet {

    //登录
    protected void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取用户名
        String username = req.getParameter("username");
        //获取密码
        String password = req.getParameter("password");
        //获取是否记住我
        String rm = req.getParameter("rm");
        System.out.println(username+":"+password+":"+rm);

        /*
        *  判定用户名和密码是否正确
        *  1.正确  -> 主页   跳转页面之前 判定是否勾选记住我  y -> 创建缓存  响应给客户端  n -> 不创建缓存
        *  2. 错误  ->登录页
        * */
        if("admin".equals(username)&&"8888".equals(password)){ // 登录成功
            if(rm!=null){ // 记住我
                //创建cookie实例进行缓存数据
                Cookie ck1 = new Cookie("username", username);
                Cookie ck2 = new Cookie("password", password);
                Cookie ck3 = new Cookie("rm", "true");

                //设置cookie的存活时间

                //ck1.setMaxAge(10);  //秒
                //ck2.setMaxAge(10);
                //ck3.setMaxAge(10);

                //将cookie对象绑定在响应对象resp对象中 缓存到客户端
                resp.addCookie(ck1);
                resp.addCookie(ck2);
                resp.addCookie(ck3);
            }else{

                Cookie ck1 = new Cookie("username", "");
                Cookie ck2 = new Cookie("password", "");
                Cookie ck3 = new Cookie("rm", "");

                //设置cookie的存活时间  0  立即删除cookie
                ck1.setMaxAge(0);  //秒
                ck2.setMaxAge(0);
                ck3.setMaxAge(0);

                //将cookie对象绑定在响应对象resp对象中 缓存到客户端
                resp.addCookie(ck1);
                resp.addCookie(ck2);
                resp.addCookie(ck3);
            }
            resp.sendRedirect("index.jsp");
        }else{ // 登录失败
            // 设置提示信息
            req.setAttribute("msg", "用户名或密码错误!");
            // 跳转页面到登录页  转发
            req.getRequestDispatcher("login.jsp").forward(req, resp);
        }

    }

    //检查用户名
    protected void checkUserName(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String username = req.getParameter("username");
        System.out.println("username = " + username);
        HashMap<String, Object> map = new HashMap<>();
        if("admin".equals(username)){
            //提示用户名已被占用  code: 500   msg:"用户名已被占用"
            map.put("code", 500);
            map.put("msg", "用户名已被占用");
        }else{
            //提示用户名可用
            map.put("code", 200);
            map.put("msg", "用户名可用");
        }
        //将数据以json格式进行响应  fastJson
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        String s = JSON.toJSONString(map);
        writer.write(s);
        writer.close();
    }
}
